﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;


namespace RISMOTAXDP.EmailService.MailProcess
{


    /// <summary>
    /// 法规解读邮件（Generate Password Email Notification）
    /// </summary>
    public class MailTaxLawSpecialExpressNotifyProcess : MailProcessBase
    {
        /// <summary>
        /// 业务类型代码
        /// </summary>
        public const string MailOPCode = "C2";

        /// <summary>
        /// 获得邮件业务类型代码
        /// </summary>
        /// <returns></returns>
        public override string GetMailOPCode()
        {
            //throw new NotImplementedException();
            return MailOPCode;
        }

        /// <summary>
        /// 执行指定邮件的生成处理
        /// </summary>
        /// <param name="index">索引号(0计数)</param>
        /// <returns></returns>
        public override bool Process(int index)
        {
            //throw new NotImplementedException();
            //从TBL_MailForward表中获得邮件
            DataTable MailForwardData = m_DataSet.Tables[0];

            //Mail_ID,OperationCode,MailSubject,MailRcpt,MailContent,MailCreateDate,MailFrom 

            Guid Mail_ID = new Guid(MailForwardData.Rows[index]["Mail_ID"].ToString());

            string OperationCode = MailForwardData.Rows[index]["OperationCode"].ToString();

            string MailSubject = MailForwardData.Rows[index]["MailSubject"].ToString();

            string MailRcpt = MailForwardData.Rows[index]["MailRcpt"].ToString();

            string MailContent = MailContentProcess(MailForwardData.Rows[index]["MailContent"].ToString());

            DateTime MailCreateDate = (DateTime)MailForwardData.Rows[index]["MailCreateDate"];

            //保存为待发送邮件
            base.SaveMail(Mail_ID, OperationCode, MailSubject, MailRcpt, MailContent, MailCreateDate);
            //移除前置邮件
            base.RemoveForwardMail(Mail_ID);

            return true;
        }

        /// <summary>
        /// 处理邮件内容
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private string MailContentProcess(string code)
        {
            //获得值并分割参数值
            //参数格式:   
            //数据ID|数据ID

            string[] parameterCode = UtilHelper.SplitString(code.Trim(), "|");

            //邮件模板内容
            string _mailContent = m_MailTemplateContent;


            if (parameterCode.Length > 0)
            {
                SqlConnection conn = GlobalObject.CreateConnect();

                //获得法规解读列表
                List<LawCase> LawCases = new List<LawCase>();

                StringBuilder commandText_Query = new StringBuilder();

                commandText_Query.Append(" select ");
                commandText_Query.Append(" ID,Title,Writer,Content,QuotationURL,DateUpdated ");
                commandText_Query.Append(" from TBL_P_HotThreads ");
                commandText_Query.Append(" where ID = @ID ");

                DataSet ds = null;

                for (int i = 0; i <= parameterCode.Length-1; i++)
                {
                    int hotThreadID = int.Parse(parameterCode[i].ToString());
                    SqlParameter[] sParms = { new SqlParameter("@ID", hotThreadID) };

                    ds = SQLHelper.ExecuteDataset(conn, CommandType.Text, commandText_Query.ToString(), sParms);

                    if (ds.Tables[0].Rows.Count != 0)
                    {
                        DataRow dataRow = ds.Tables[0].Rows[0];
                        LawCase _lawCase = new LawCase();
                        _lawCase.ID = (int)dataRow["ID"];
                        _lawCase.Title = dataRow["Title"].ToString();
                        _lawCase.Writer = dataRow["Writer"].ToString();
                        _lawCase.Content = dataRow["Content"].ToString();
                        _lawCase.QuotationURL = dataRow["QuotationURL"].ToString();
                        _lawCase.DateUpdated = (DateTime)dataRow["DateUpdated"];

                        LawCases.Add(_lawCase);
                    }
                }
                GlobalObject.CloseConnect(conn);


                //开始分析模板标签
                Match m;

                //分析<templateRepeatList></templateRepeatList>标签
                m = MailTemplateObject.regex_RepeatList.Match(_mailContent);

                //未找到
                if (!m.Success)
                {
                    return _mailContent;
                }

                //当有一个的时候
                //包含标签
                string s_CodeContent = m.Groups[0].ToString();
                //不包含标签名
                string s_NotCodeContent = m.Groups[1].ToString();


                string d_Content = string.Empty;

                foreach (LawCase _lawCase in LawCases)
                {
                    string t_Content = s_NotCodeContent;
                    
                    //开始替换
                    t_Content = t_Content.Replace(MailTemplateObject.LawCaseTitle, _lawCase.Title);
                    t_Content = t_Content.Replace(MailTemplateObject.LawCaseWriter, _lawCase.Writer);
                    t_Content = t_Content.Replace(MailTemplateObject.LawCaseTitleURL, _lawCase.QuotationURL);
                    t_Content = t_Content.Replace(MailTemplateObject.LawCaseDate, Convert.ToDateTime(_lawCase.DateUpdated).ToString("yyyy-MM-dd"));

                    d_Content = d_Content + t_Content;
                }

                _mailContent = _mailContent.Replace(s_CodeContent, d_Content);

            }
            else
            {
                _mailContent = string.Empty;
            }



            return _mailContent;

        }


 


    }


    /// <summary>
    /// 法规解读对象
    /// </summary>
    public class LawCase
    {
        public LawCase() { }
        #region

        public int ID { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// 作者
        /// </summary>
        public string Writer { get; set; }
        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; }
        /// <summary>
        /// 链接地址URL
        /// </summary>
        public string QuotationURL { get; set; }
        /// <summary>
        /// 发布日期
        /// </summary>
        public DateTime DateUpdated { get; set; }
        #endregion
    }

 

}
